cmake_minimum_required(VERSION 3.0)
project(ParaViewTranslations NONE)
#搜索Qt5的翻译包
find_package(Qt5 REQUIRED QUIET COMPONENTS Core LinguistTools)
#设置安装路径
set(INSTALL_SUBDIR "share/translations" CACHE STRING "Destination of the 
  result files within the INSTALL_PREFIX directory")
#设置构建安装qt翻译文件选项
option(PROVIDE_QT_TRANSLATIONS "Build and install official Qt translation files" OFF)
#如果设置了同时翻译qt文件选项
if (PROVIDE_QT_TRANSLATIONS)
  set(qt_ts_prefix
    qt
    qtbase
    qtscript
    qtmultimedia
    qtxmlpatterns)
endif ()
#设置需要翻译的ts文件
set(ts_files
  Clients_ParaView.ts
  Qt_ApplicationComponents.ts
  Qt_Core.ts
  Qt_Widgets.ts
  Clients_ParaView-XMLs.ts
  Qt_Components.ts
  Qt_Python.ts
  ServerManager-XMLs.ts
  )
#设置语言
set(languages
  zh_CN
  )
#添加自定义目标
add_custom_target("files_update")
#遍历语言
foreach (output_language IN LISTS languages)
  if (PROVIDE_QT_TRANSLATIONS)
    # Get the language code without the country code
    string(REGEX MATCH "^([a-z]+)"
      _language_code "${output_language}")
    #翻译qt 组件
    foreach (_qt_component IN LISTS qt_ts_prefix)
      if ("${output_language}" STREQUAL "en")
        # Skip en as qt does not provide it.
        # 如果是英文不需要翻译
        continue()
      endif()
      # Create qm generation targets
      set(_qt_ts "${_qt_component}_${_language_code}")
      set(source_ts "${CMAKE_SOURCE_DIR}/qttranslations/translations/${_qt_ts}.ts")
      if (NOT EXISTS "${source_ts}")
        set(_qt_ts "${_qt_component}_${output_language}")
        set(source_ts "${CMAKE_SOURCE_DIR}/qttranslations/translations/${_qt_ts}.ts")
      endif()

      set(destination_qt_qm "${CMAKE_BINARY_DIR}/${_qt_ts}.qm")

      add_custom_command(
        OUTPUT "${destination_qt_qm}"
        COMMAND "$<TARGET_FILE:Qt5::lconvert>" ${source_ts} -o "${destination_qt_qm}"
        DEPENDS "${source_ts}")
      add_custom_target("${_language_code}_${_qt_ts}" ALL DEPENDS "${destination_qt_qm}")
      install(
        FILES "${destination_qt_qm}"
        DESTINATION "${INSTALL_SUBDIR}"
      )
    endforeach ()
  endif ()

  set(ts_language_files ${ts_files})
  list(TRANSFORM ts_language_files PREPEND "${CMAKE_CURRENT_LIST_DIR}/${output_language}/")
  # Create qm generation targets
  set(destination_qm "${CMAKE_BINARY_DIR}/paraview_${output_language}.qm")
  add_custom_command(
    OUTPUT "${destination_qm}"
    COMMAND "$<TARGET_FILE:Qt5::lconvert>" ${ts_language_files} -o "${destination_qm}"
    DEPENDS ${ts_language_files})
  add_custom_target("${output_language}" ALL DEPENDS "${destination_qm}")
  if ("${output_language}" STREQUAL "en")
    # en files will never be used by ParaView
    # 如果英文不需要安装
    continue()
  endif()
  install(
    FILES "${destination_qm}"
    DESTINATION "${INSTALL_SUBDIR}"
  )

  # Create ts files update target
  if ("${output_language}" STREQUAL "en")
    # Skip en as they are the template files
    #如果是英文跳过
    continue()
  endif()
  add_custom_target("${output_language}_update")
  foreach (ts_file IN LISTS ts_files)
    set(absolute_ts_file "${CMAKE_SOURCE_DIR}/${output_language}/${ts_file}")
    set(absolute_en_file "${CMAKE_SOURCE_DIR}/en/${ts_file}")
    add_custom_target("${output_language}_${ts_file}_update")
    add_custom_command(
      TARGET "${output_language}_${ts_file}_update"
      COMMAND "$<TARGET_FILE:Qt5::lupdate>" "${absolute_en_file}" -ts "${absolute_ts_file}" -target-language ${output_language}
      )
    add_custom_command(TARGET "${output_language}" POST_BUILD
      #COMMAND "${CMAKE_COMMAND}" -E copy ${destination_qm} "${CMAKE_BINARY_DIR}/share/translations"
      COMMAND "${CMAKE_COMMAND}" -E copy ${destination_qm} "${CMAKE_BINARY_DIR}/translations"
    )
    add_dependencies("${output_language}_update" "${output_language}_${ts_file}_update")
  endforeach ()
  #添加files_update优先构建
  add_dependencies("files_update" "${output_language}_update")
endforeach ()